import request from '../request.ts'

//获取
export const getUserList = async  (search:Object) => {
    let r:any = await request({
        url: '/system/user/list',
        method: 'get',
        loading: true,
        data:search
    })
    return {
        data:r.data.rows,
        total:r.data.total
    }
}

//新增
export const addUser = async  (data:Object) => {
    let r:any = await request({
        url: '/system/user/edit',
        method: 'post',
        loading: true,
        data:data
    })
    return r
}
//修改
export const editUser = async  (data:Object) => {
    let r:any = await request({
        url: '/system/user/edit',
        method: 'put',
        loading: true,
        data:data
    })
    return r
}
//删除
export const delUser = async  (id:Number) => {
    let r:any = await request({
        url: '/system/user/delete',
        method: 'delete',
        loading: true,
        data:{
            userId:id
        }
    })
    return r
}
//获取角色的资源权限
export const getRoleMenuTreeselect = async  (roleId) => {
    let r:any = await request({
        url: '/system/menu/roleMenuTreeselect/' + roleId,
        method: 'get',
        loading: true
    })
    return {
        menus:r.menus,
        checkedKeys:r.checkedKeys
    }
}
//获取用户信息及下拉信息
export const getUserSelects = async  (userId = null) => {
    let r:any = await request({
        url: '/system/user/userInfo',
        method: 'get',
        loading: true,
        data:{
            userId:userId
        }
    })
    return {
        posts:r.data.posts,
        roles:r.data.roles,
        postIds:r.data.postIds,
        roleIds:r.data.roleIds
    }
}

//初始化密码
import config from '@/config.ts'
export const resetPwdUser = async  (userId:String) => {
    let r:any = await request({
        url: '/system/user/resetPwd',
        method: 'put',
        loading: true,
        data:{
            password:config.userResetPwd,
            userId:userId
        }
    })
    return r
}

//修改用户信息
export const userModify = (data) => {
    return request({
        url: '/system/user/edit',
        method: 'put',
        loading: true,
        data:data
    })
}
//修改密码
export const userModifyPassword = (oldPassword, newPassword) => {
    return request({
        url: `/system/user/updatePwd`,
        method: 'post',
        loading: true,
        data:{
            oldPassword:oldPassword,
            newPassword:newPassword
        }
    })
}
//修改头像 /system/user/profile/avatar
export const userModifyAvatar = (avatar) => {
    return request({
        url: `/system/user/profile/updateAvatar?avatar=${avatar}`,
        method: 'post',
        loading: true,
    })
}